package com.ruoyu.cloud.ai.config;

import org.springframework.ai.autoconfigure.vectorstore.redis.RedisVectorStoreAutoConfiguration;
import org.springframework.ai.autoconfigure.vectorstore.redis.RedisVectorStoreProperties;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.providers.PooledConnectionProvider;

@Configuration
@EnableAutoConfiguration(exclude = {
        RedisVectorStoreAutoConfiguration.class
})
@EnableConfigurationProperties({
        RedisVectorStoreProperties.class
})
public class RedisConfig {

    /*** 创建RedisStack向量数据库** @param embeddingModel 嵌入模型* @param properties     redis-stack的配置信息* @return vectorStore 向量数据库*/
    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel,
                                   RedisVectorStoreProperties properties,
                                   RedisConnectionDetails redisConnectionDetails
    ) {
        /*JedisPooled jedisPooled = new JedisPooled(redisConnectionDetails.getStandalone().getHost(),
                redisConnectionDetails.getStandalone().getPort(),
                redisConnectionDetails.getUsername(),
                redisConnectionDetails.getPassword());*/
        HostAndPort hostAndPort = new HostAndPort("192.168.4.86", 6379);
        DefaultJedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
                .password("123456").build();
        PooledConnectionProvider pooledConnectionProvider = new PooledConnectionProvider(hostAndPort, clientConfig);
        JedisPooled jedisPooled = new JedisPooled(pooledConnectionProvider);
        RedisVectorStore redisVectorStore =
                RedisVectorStore.builder(jedisPooled, embeddingModel)
                        .indexName("faqs")
                        .prefix("faq")
                        .initializeSchema(properties.isInitializeSchema())
                        .build();
        return redisVectorStore;
    }


}
